package org.ns.summer.classpath.scan.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ns.summer.classpath.scan.ScanFile;
import org.ns.summer.classpath.scan.ScanFileExecute;
import org.ns.summer.classpath.scan.ScanFileUtils;
import org.ns.summer.classpath.scan.SuffixScanFileParse;
import org.ns.summer.classpath.scan.prefix.PrefixTree;

/**
 * @author 张星宇
 *
 * @email m.vip@qq.com
 */
public class BasicScanFileExecute implements ScanFileExecute {
	protected Map<String, SuffixScanFileParse> suffixScanFileParses = new HashMap<String, SuffixScanFileParse>();
	private PrefixTree prefixTree = new PrefixTree();
	private boolean prefixTreeEmpty = true; //  前缀树为空，则不匹配前缀
	@Override
	public void addSuffixScanFileParse(String suffix, SuffixScanFileParse scanFileParse) {
		suffixScanFileParses.put(suffix, scanFileParse);
	}

	@Override
	public List<ScanFile> execScan(String rootPath) {
		rootPath = ScanFileUtils.getFileString(rootPath);
		File rootFile = new File(rootPath);
		List<ScanFile> scanFiles = new ArrayList<ScanFile>();
		if (rootFile.isFile()) {
			List<ScanFile> scanSubFiles = scanIsFile(rootPath, rootFile);
			for (ScanFile scanSubFile : scanSubFiles) {
				if (match(scanSubFile.getFilePath())) {
					scanFiles.add(scanSubFile);
				}
			}
			ScanFileUtils.updateScanClassInfo(scanFiles);
			return scanFiles;
		}
		List<File> dirFiles = new ArrayList<File>();
		dirFiles.add(rootFile);
		for (int i = 0; i < dirFiles.size(); i++) {
			File dirFile = dirFiles.get(i);
			File[] subFiles = dirFile.listFiles();
			for (File subFile : subFiles) {
				if (subFile.isDirectory()) {
					dirFiles.add(subFile);
				} else {
					List<ScanFile> scanSubFiles = scanIsFile(rootPath, subFile);
					if (scanSubFiles != null && scanSubFiles.size() > 0) {
						for (ScanFile scanSubFile : scanSubFiles) {
							if (match(scanSubFile.getFilePath())) {
								scanFiles.add(scanSubFile);
							}
						}
					}
				}
			}
		}
		// 增加 classinfo 信息
		ScanFileUtils.updateScanClassInfo(scanFiles);
		return scanFiles;
	}

	@Override
	public ScanFileExecute addPrefixLimit(String[] prefixs) {
		if (prefixs != null && prefixs.length > 0) {
			prefixTree.adds(prefixs);
			prefixTreeEmpty = false;
		}
		return this;
	}

	boolean match(String path) {
		if (prefixTreeEmpty) {
			return true;
		}
		return prefixTree.match(path);
	}

	// 对文件扫描呵呵
	protected List<ScanFile> scanIsFile(String rootPath, File file) {
		String absolutePath = file.getAbsolutePath();
		String suffixName = ScanFileUtils.getSuffixString(absolutePath);
		SuffixScanFileParse suffixScanFileParse = suffixScanFileParses.get(suffixName);
		if (suffixScanFileParse != null) {
			// 通过后缀进行扫描
			return suffixScanFileParse.parseScanFiles(rootPath, absolutePath, suffixName);
		} else {
			String filePath = absolutePath.substring(rootPath.length());
			return Arrays.asList(new ScanFile(rootPath, filePath, absolutePath));
		}
	}

	@Override
	public SuffixScanFileParse getSuffixScanFileParse(String suffix) {
		// 获得文件后缀解析器。
		return suffixScanFileParses.get(suffix);
	}

}
